#!/bin/sh
#chkconfig: 2345 80 05
#description: admin-server

#jdk
#export JAVA_HOME=/usr/local/jdk1.8.0_192
#export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin

#设置格式
:set fileformat=unix

#根目录
DIR=/home/daison/encrypt/script

#1、检查admin服务
function check_admin(){
	SERVER_NAME=daison-admin-server
	ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
	   echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
	   sudo sh $DIR/admin-server.sh stop
	   sudo sh $DIR/admin-server.sh start
	fi
}


#2、检查beacon-server服务
function check_server(){
	SERVER_NAME=daison-server
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/beacon-server.sh stop
           sudo sh $DIR/beacon-server.sh start
        fi
}

#3、检查gateway服务
function check_gateway(){
        SERVER_NAME=daison-gateway
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/gateway-server.sh stop
           sudo sh $DIR/gateway-server.sh start
        fi
}

#4.检查thirdpaltform服务
function check_thirdplatform(){
        SERVER_NAME=daison-thirdplatform
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/thirdplatform-server.sh stop
           sudo sh $DIR/thirdplatform-server.sh start
        fi
}

#5.检查websocket服务
function check_websocket(){
        SERVER_NAME=daison-websocket
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/websocket-server.sh stop
           sudo sh $DIR/websocket-server.sh start
        fi
}


#6.检查beacon-task服务
function check_task(){
        SERVER_NAME=daison-beacon-task-1.0.1-SNAPSHOT.jar
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/task-server.sh stop
           sudo sh $DIR/task-server.sh start
        fi
}

#7.检查tunnel-ipc服务
function check_tunnel_ipc(){
        SERVER_NAME=daison-tunnel-ipc
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/ipc-server.sh stop
           sudo sh $DIR/ipc-server.sh start
        fi
}

#8.信标上次服务
function check_uplink(){
        SERVER_NAME=daison-uplink
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/uplink-server.sh stop
           sudo sh $DIR/uplink-server.sh start
        fi	
}

#9.数据分析服务
function check_analysis(){
        SERVER_NAME=daison-analysis
        ps -ef | grep $SERVER_NAME | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sudo sh $DIR/analysis-server.sh stop
           sudo sh $DIR/analysis-server.sh start
        fi
}



#检查服务开启
t=$(date "+%Y-%m-%d %H:%M:%S")
echo "check server start ............"
echo "check time: $t"

#while true
#do
cat <<EOF
****************************************
	the flowing is optional
*****************************************
	1) daison-admin
	2) daison-server
	3) daison-gateway
	4) daison-websocket
	5) daison-thridplatfrom
	6) daison-task-qinling
	7) daison-tunnel-ipc
	8) daison-uplink
	9) daison-analysis
	97)
	    check_admin
            check_server
            check_gateway
            check_websocket
            check_thirdplatform
	    check_task
	    check_uplink
	98)
	    daison-task-qinling	
	99) 全部服务(1-10)
****************************************
EOF

#read -p "please enter your chioce:" option
case "$1" in
		1)
		   check_admin
		   ;;
		2)
		   check_server
		   ;;
		3)
		   check_gateway
		   ;;
		4)
		   check_websocket
		   ;;
		5)
		   check_thirdplatform
		   ;;
		6)
		   check_task
		   ;;
		7)
		   check_tunnel_ipc
		   ;;
		8)
		   check_uplink
		   ;;
		9)
		   check_analysis
		   ;;
		97)
		   check_admin
                   check_server
		   check_uplink
                   check_gateway
                   check_websocket
                   check_thirdplatform
		   check_task
		   ;;
		98)
		   check_task
		   check_task_qinling
		   ;;	
		99)
		   check_admin
		   check_server
		   check_gateway
		   check_websocket
		   check_thirdplatform
		   check_task
		   check_uplink
		   check_analysis
		   check_tunnel_ipc
		   check_tunnel_task
		  ;;
		*)
		   echo "option is inviald."
		   echo "usage: /home/daison/admin/script/check-server.sh {1-99}" >&2
		   exit 1
esac
#done
